package com.shanghaionstar.user;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.exception.HttpException;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.more.entity.SubscriberDTO;
import com.onstar.cn.vehicle.entity.ErrorInfo;
import com.onstar.cn.weight.DeleteEditText;
import com.shanghaionstar.R;
import com.shanghaionstar.user.iview.IPayCardVisitorActivieView;
import com.shanghaionstar.user.presenter.PayCardVisitorActivePresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ErrorCodeUtil;
import com.shanghaionstar.weight.TopLayout;

import java.util.Locale;

/**
 * Created by Mengzhou Gu on 2016/8/4.
 */
public class PayCardVisitorActiveFragment extends BaseFragment implements View.OnFocusChangeListener,IPayCardVisitorActivieView {
    private PrePayActivity parentActivity     = null;
    @FindViewById
    private TopLayout      top_layout         = null;
    @FindViewById
    private DeleteEditText et_car_identify    = null;// 车辆识别码
    @FindViewById
    private DeleteEditText phone_number       = null;// 手机号码
    @FindViewById
    private TextView       car_number_error   = null;
    @FindViewById
    private TextView       phone_number_error = null;
    private ProgressDialog dialog;
    @FindViewById
    private RelativeLayout layout_car_iden    = null;
    private PayCardVisitorActivePresenter presenter;
    @FindViewById
    private TextView right_btn_text;

    @Override
    public int getLayoutId() {
        return R.layout.user_paycard_visitor_fragment;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.et_car_identify:
                if (!hasFocus) {
                    // 格式校验
                    if (!et_car_identify.getText().toString().trim().equals("")) {
                        if (!CommonUtil.isVin(et_car_identify.getText().toString().trim())) {
                            car_number_error.setVisibility(View.VISIBLE);
                            car_number_error.setText(getString(R.string.SB027_MSG003));
                        }
                    } else {
                        car_number_error.setVisibility(View.VISIBLE);
                        car_number_error.setText(getString(R.string.SB027_MSG004));
                    }
                } else {
                    car_number_error.setVisibility(View.GONE);
                }
                break;
            case R.id.phone_number:
                if (!hasFocus) {
                    // 格式校验
                    if (!phone_number.getText().toString().trim().equals("")) {
                        if (!CommonUtil.isPhone(phone_number.getText().toString().trim())) {
                            phone_number_error.setVisibility(View.VISIBLE);
                            phone_number_error.setText(getString(R.string.phone_format_error));
                        }
                    } else {
                        phone_number_error.setVisibility(View.VISIBLE);
                        phone_number_error.setText(getString(R.string.SB027_MSG004));
                    }
                } else {
                    phone_number_error.setVisibility(View.GONE);
                }
                break;
            default:
                break;
        }
        DeleteEditText clearText = (DeleteEditText) v;
        clearText.hasFocus(hasFocus);
        if (hasFocus) {
            clearText.setDeleteIconVisible(clearText.getText().length() > 0);
        } else {
            clearText.setDeleteIconVisible(false);
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        parentActivity = (PrePayActivity) getActivity();
        parentActivity.current_fragment = "PayCardVisitorActiveFragment";

        presenter = new PayCardVisitorActivePresenter(parentActivity.getScheduler(), parentActivity, et_car_identify);
        presenter.setView(this);
        et_car_identify.setOnFocusChangeListener(this);
        phone_number.setOnFocusChangeListener(this);
        top_layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                CommonUtil.hideKeyBoard(getActivity(), phone_number);
                parentActivity.finish();
            }
        });

        top_layout.setOnRightClickListener(getResources().getString(R.string.next_step), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout_car_iden.setFocusable(true);
                layout_car_iden.setFocusableInTouchMode(true);
                layout_car_iden.requestFocus();

                CommonUtil.hideKeyBoard(getActivity(), phone_number);
                if (!CommonUtil.isNull(et_car_identify.getText().toString())
                        || !CommonUtil.isNull(phone_number.getText().toString())) {
                    if (!CommonUtil.isVin(et_car_identify.getText().toString().trim())
                            || !CommonUtil.isPhone(phone_number.getText().toString().trim())) {
                        return;
                    }
                  //  getAccountInfoByVin();
                    presenter.getData();
                } else {
                    if (et_car_identify.getText().length() <= 0) {
                        phone_number_error.setText(getString(R.string.SB027_MSG004));
                        phone_number_error.setVisibility(View.VISIBLE);
                    }
                    if (phone_number.getText().length() <= 0) {
                        car_number_error.setVisibility(View.VISIBLE);
                        car_number_error.setText(getString(R.string.SB027_MSG004));
                    }
                }
            }
        });
        top_layout.setRightTextBg(R.drawable.onstar_personal_btn);
        right_btn_text.setTextColor(Color.WHITE);
 //       top_layout.setRightTextBg(R.drawable.paycard_use_ppc_next_selector);

     /*   top_layout.setRightButtonClickListener(R.string.next_step, R.drawable.onstar_use_ppc_next_selector,
                new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        layout_car_iden.setFocusable(true);
                        layout_car_iden.setFocusableInTouchMode(true);
                        layout_car_iden.requestFocus();

                        CommonUtil.hideKeyBoard(getActivity(), phone_number);
                        if (!CommonUtil.isNull(et_car_identify.getText().toString())
                                || !CommonUtil.isNull(phone_number.getText().toString())) {
                            if (!CommonUtil.isVin(et_car_identify.getText().toString().trim())
                                    || !CommonUtil.isPhone(phone_number.getText().toString().trim())) {
                                return;
                            }
                            getAccountInfoByVin();
                        } else {
                            if (et_car_identify.getText().length() <= 0) {
                                phone_number_error.setText(getString(R.string.SB027_MSG004));
                                phone_number_error.setVisibility(View.VISIBLE);
                            }
                            if (phone_number.getText().length() <= 0) {
                                car_number_error.setVisibility(View.VISIBLE);
                                car_number_error.setText(getString(R.string.SB027_MSG004));
                            }
                        }

                    }
                });*/
    }


//    public void changeFragment(FragmentManager fragmentManager, Fragment fragment, String tag) {
//        if (null != fragmentManager.findFragmentByTag(tag))
//            return;
//        FragmentTransaction p_FragmentTransaction = fragmentManager.beginTransaction();
//        p_FragmentTransaction.replace(R.id.fragment_container, fragment, tag);
//        p_FragmentTransaction.addToBackStack(null);
//        p_FragmentTransaction.commitAllowingStateLoss();
//    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        top_layout = null;
    }

    @Override
    public void showSuccess(SubscriberDTO subscriberDTO) {
        if (subscriberDTO != null) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("account_obj", subscriberDTO);
            bundle.putString("vin", et_car_identify.getText().toString().toUpperCase(Locale.CHINA).trim());
            UsePrepayCardFragment userprepay = new UsePrepayCardFragment();
            userprepay.setArguments(bundle);
            parentActivity.changeFragment(parentActivity.fragmentManager, userprepay, UsePrepayCardFragment.class,true);
        }
    }

    @Override
    public void showFailure(Bundle bundle) {
        ErrorInfo errorInfo = (ErrorInfo) bundle.getSerializable("errorInfo");
        ErrorCodeUtil.ShowErrorSompMsg(getActivity(), errorInfo);
    }

    @Override
    public void showError(Throwable throwable,Bundle bundle) {
        AlertDialogUtil.showDialogByExceptionCode(parentActivity, (HttpException) throwable);
        ErrorInfo ErrorInfo = (ErrorInfo) bundle.getSerializable("errorInfo");
        ErrorCodeUtil.ShowErrorSompMsg(getActivity(), ErrorInfo);
    }

    @Override
    public void setLoading(boolean isLoading) {
        if(isLoading){
            dialog=showLoadProgress();
            dialog.show();
        }else {
            dismissDialog(dialog);
        }
    }

    @Override
    public void onResponseError(OnStarException e) {

    }

    @Override
    public void onReponseSuccess(SubscriberDTO object) {

    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }
}
